package com.github.eventbusdemo;

import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;

import androidx.appcompat.app.AppCompatActivity;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

public class ReceiveActivity extends AppCompatActivity implements View.OnClickListener {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_receive);
        EventBus.getDefault().register(ReceiveActivity.this);
        initUI();
    }

    private void initUI() {
        Button sendBtn = findViewById(R.id.send_btn_id);
        sendBtn.setOnClickListener(this);
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.send_btn_id:
                sendMsg();
                break;
        }
    }

    private void sendMsg() {
        MessageWrap wrap = new MessageWrap();
        wrap.setMessage("hello world");
        EventBus.getDefault().post(wrap);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        EventBus.getDefault().unregister(ReceiveActivity.this);
        //EventBus.getDefault().removeAllStickyEvents();
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onMessageWrap(MessageWrap wrap) {
        Toast.makeText(this, wrap.getMessage(), Toast.LENGTH_SHORT).show();
    }
}